//
//  KAEImagePickerController.m
//  StudyDemo
//
//  Created by 陶涛 on 2018/8/9.
//  Copyright © 2018年 陶涛. All rights reserved.
//

#import "KAEImagePickerController.h"
#import <MobileCoreServices/MobileCoreServices.h>

@interface KAEImagePickerController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property (nonatomic, strong) UIImagePickerController *imageController;

@end

@implementation KAEImagePickerController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUI];
}

- (void)setUI {
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeSystem];
    [btn1 setTitle:@"拍摄照片" forState:UIControlStateNormal];
    [btn1 addTarget:self action:@selector(takephoto) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn1];
    
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeSystem];
    [btn2 setTitle:@"拍摄视频" forState:UIControlStateNormal];
    [btn2 addTarget:self action:@selector(takeVideo) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn2];
    
    UIButton *btn3 = [UIButton buttonWithType:UIButtonTypeSystem];
    [btn3 setTitle:@"选择照片" forState:UIControlStateNormal];
    [btn3 addTarget:self action:@selector(pickerImage) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn3];
    
    [btn1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.mas_topLayoutGuide).offset(20);
    }];
    [btn2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(btn1.mas_bottom).offset(20);
    }];
    [btn3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(btn2.mas_bottom).offset(20);
    }];
}

- (UIImagePickerController *)imageController {
    if (!_imageController) {
        _imageController = [[UIImagePickerController alloc] init];
        _imageController.delegate = self;
    }
    return _imageController;
}

- (void)takeVideo {
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        self.imageController.sourceType = UIImagePickerControllerSourceTypeCamera;
        self.imageController.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
        self.imageController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo;
        [self presentViewController:self.imageController animated:YES completion:NULL];
    }
}

- (void)takephoto {
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        self.imageController.sourceType = UIImagePickerControllerSourceTypeCamera;
        self.imageController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
        //    self.imageController.cameraDevice = UIImagePickerControllerCameraDeviceRear;
        self.imageController.allowsEditing = YES;
        [self presentViewController:self.imageController animated:YES completion:NULL];
    }
    
}

- (void)pickerImage {
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
        self.imageController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        self.imageController.allowsEditing = NO;
        [self presentViewController:self.imageController animated:YES completion:NULL];
    }
}

#pragma mark - UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    NSString *mediaType = info[@"UIImagePickerControllerMediaType"];
    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
        //选取的是图片
        NSLog(@"获取的资源是图片");
    } else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]) {
        NSLog(@"获取的资源是视频");
    }
    [picker dismissViewControllerAnimated:YES completion:NULL];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:NULL];
}


@end
